/*
    This file is part of Darwin.

    Darwin is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    
    Darwin uses GALib
    Copyright (c) 1995-1996 Massachusetts Institute of Technology (MIT)
    Copyright (c) 1996-2005 Matthew Wall
    All rights reserved.
    
    Darwin uses ConfigFile
    Copyright (c) 2004 Richard J. Wagner
     
    Darwin, a Genetic Algorithm to solve Timetabling problem
    Copyright (c) 2006 Christian S. Perone
*/  

#include "dbanalysis.h"

extern mysqlpp::Connection g_connection;
extern GlobalOptions g_options;

int GetNiveis(void)
{
    int niveis=0;
    mysqlpp::Query query = g_connection.query();

    query << "SELECT COUNT(DISTINCT nivel) niveis FROM OFERECIMENTO WHERE AnoSemestre = " << g_options.timetabling_anosemestre 
          << " AND CodCurso = " << g_options.timetabling_curso;
    mysqlpp::Result resnivel = query.store();
    mysqlpp::Row rownivel;
    rownivel = resnivel.at(0);
    niveis = rownivel["niveis"];
    query.reset();
    
    return niveis;
}

int GetTimeSlots(void)
{
    int timeslots=0;
    mysqlpp::Query query = g_connection.query();

    query << "SELECT COUNT(*) timeslots FROM TIMESLOT WHERE AnoSemestre = " 
          << g_options.timetabling_anosemestre;
    mysqlpp::Result restimeslot = query.store();
    mysqlpp::Row rowtimeslot;
    // Just one line, so we can use fixed index
    rowtimeslot = restimeslot.at(0);
    timeslots = rowtimeslot["timeslots"];
    query.reset();

    return timeslots;
}

int GetWeekdayTimeslotSize(void)
{
     int timeslotsize=0;
     mysqlpp::Query query = g_connection.query();
     
     query << "SELECT CodDiaSemana, COUNT(*) total FROM TIMESLOT GROUP BY CodDiaSemana LIMIT 1";
     mysqlpp::Result res = query.store();
     mysqlpp::Row row;
     row = res.at(0);

     timeslotsize = row["total"];
     query.reset();
     
     return timeslotsize;
}
